﻿Option Explicit On

Imports System.Text
Imports System.Data
Imports System.ComponentModel
Imports Global_Visas_Business_Layer

Public Class frmAdminLetterRegister

    Private Sub AdminLetterRegister_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        'Dim ClientType As Integer
        'Dim LetterType As Integer
        'Dim OutputType As Integer

        'If Me.radType.Checked Then
        '    LetterType = 0
        'Else
        '    LetterType = Me.drpLetterType.SelectedValue
        'End If

        'If Me.radClientAll.Checked Then
        '    ClientType = 0
        'ElseIf Me.radClientPrivate.Checked Then
        '    ClientType = 1
        'ElseIf Me.radClientCorp.Checked Then
        '    ClientType = 2
        'End If

        'If Me.radOutputAll.Checked Then
        '    OutputType = 0
        'Else
        '    OutputType = Me.drpOutputType.SelectedValue
        'End If

        With Me.drpLetterType
            .DisplayMemberPath = "LetterType_Name"
            .SelectedValuePath = "LetterType_ID"
            .ItemsSource = CType(Letters.LetterTypeList, IListSource).GetList
            .SelectedIndex = -1
        End With

        With Me.drpOutputType
            .DisplayMemberPath = "text"
            .SelectedValuePath = "id"
            .ItemsSource = CType(Me.OutputTypes, IListSource).GetList
            .SelectedIndex = -1
        End With

        'Me.grdLetters.DataSource = Letters.StockLetterList(ClientType, LetterType, OutputType)
        Me.LoadData(Me, Nothing)

        AddHandler drpLetterType.SelectionChanged, AddressOf LoadData
        AddHandler drpOutputType.SelectionChanged, AddressOf LoadData
        AddHandler radTypeAll.Checked, AddressOf LoadData
        AddHandler radClientAll.Checked, AddressOf LoadData
        AddHandler radClientCorp.Checked, AddressOf LoadData
        AddHandler radClientPrivate.Checked, AddressOf LoadData

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub


    Private Sub grdLetters_SelectedRowsChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdLetters.SelectionChanged
        If Me.grdLetters.SelectedItems.Count > 0 Then
            Dim selectedRow As DataRow = Me.grdLetters.SelectedItem
            DataBinding.BindListView(Letters.LetterUsage(selectedRow(0)), Me.lvwWhereUsed)
        End If
    End Sub

    Private Function OutputTypes() As DataTable

        Dim myTable As New DataTable
        Dim myCol As DataColumn
        Dim myRow As DataRow

        myCol = New DataColumn("id", System.Type.GetType("System.Int16"))
        myTable.Columns.Add(myCol)
        myCol = New DataColumn("text", System.Type.GetType("System.String"))
        myTable.Columns.Add(myCol)

        myRow = myTable.NewRow
        myRow.Item("id") = "1"
        myRow.Item("text") = "Print"
        myTable.Rows.Add(myRow)

        myRow = myTable.NewRow
        myRow.Item("id") = "2"
        myRow.Item("text") = "Email"
        myTable.Rows.Add(myRow)

        myRow = myTable.NewRow
        myRow.Item("id") = "3"
        myRow.Item("text") = "File"
        myTable.Rows.Add(myRow)

        Return myTable


    End Function

    Private Sub LetterType_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radType.Checked, radTypeAll.Checked


        If CType(sender, RadioButton).Name = "radType" Then
            Me.drpLetterType.IsEnabled = True
        Else
            Me.drpLetterType.IsEnabled = False
        End If

    End Sub

    Private Sub OutputType_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radOutputAll.Checked, radOutputList.Checked

        If CType(sender, RadioButton).Name = "radOutputAll" Then
            Me.drpOutputType.IsEnabled = False
        Else
            Me.drpOutputType.IsEnabled = True
        End If

    End Sub


    Private Sub LoadData(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim ClientType As Integer
        Dim LetterType As Integer
        Dim OutputType As Integer

        If Me.radTypeAll.IsChecked Then
            LetterType = 0
        Else
            LetterType = Me.drpLetterType.SelectedValue
        End If

        If Me.radClientAll.IsChecked Then
            ClientType = 0
        ElseIf Me.radClientPrivate.IsChecked Then
            ClientType = 1
        ElseIf Me.radClientCorp.IsChecked Then
            ClientType = 2
        End If

        If Me.radOutputAll.IsChecked Then
            OutputType = 0
        Else
            OutputType = Me.drpOutputType.SelectedValue
        End If

        Me.grdLetters.ItemsSource = Letters.StockLetterList(ClientType, LetterType, OutputType)
    End Sub


    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim formAdminNewLetterReg As New frmAdminNewLetterReg
        formAdminNewLetterReg.ShowDialog()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim myRow As DataRow = Me.grdLetters.SelectedItem
        If myRow Is Nothing Then
            MsgBox("Please select a letter first", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If



        If myRow(5) > 0 Then
            MsgBox("Cannot delete, that letter is still in use", MsgBoxStyle.Exclamation, "Error")
            Exit Sub
        End If

        Dim Msg As New StringBuilder

        With Msg
            .Append("Are you sure you wish to delete the following letter registration?")
            .Append(vbCrLf & vbCrLf & myRow(1) & vbCrLf & vbCrLf)
            .Append("Note: The actual template file on the F: drive will NOT be deleted, only the database registration")
        End With

        If MsgBox(Msg.ToString, vbYesNo, "Confirm delete") <> MsgBoxResult.Yes Then Exit Sub

        Msg = New StringBuilder

        Msg.Append("Letter registration '" & myRow(1) & "' (" & myRow(0) & ") deleted")

        If Not Letters.DeleteLetter(myRow(0), Msg.ToString) Then
            Exit Sub
        End If

        Me.LoadData(Me, Nothing)
        'Me.grdLetters.SelectedRows.Clear()



    End Sub
End Class